package com.gxa.index.controller;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.common.base.RespResult;
import com.gxa.common.utils.GenerateSequenceUtil;
import com.gxa.index.DTO.SetServiceTypeDTO;
import com.gxa.index.entity.*;

import com.gxa.index.entity.SetService;
import com.gxa.index.service.*;
import com.gxa.index.vo.AccommodationVo;
import com.gxa.index.vo.HeadlinesVo;
import com.gxa.index.vo.ReservationVo;
import com.gxa.index.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/index")
@Transactional
public class IndexController {

    @Autowired
    private SetServiceTypeService setServiceTypeService;
    @Autowired
    private com.gxa.index.service.SetService setServices;
    @Autowired
    private ServiceDetailsService serviceDetailsService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private BussinessService bussinessService;
    @Autowired
    private IndustryService industryService;
    @Autowired
    private FieldService fieldService;
    @Autowired
    private BusinessCustomerService businessCustomerService;
    @Autowired
    private AccommodationService accommodationService;
    @Autowired
    private AccommodationCommentsService accommodationCommentsService;
    @Autowired
    private AccommodationCommentsImageService accommodationCommentsImageService;
    @Autowired
    private AccommodationOrderService accommodationOrderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private AccommodationRoomTypeService accommodationRoomTypeService;
//
//服务大厅-显示所有服务
    @GetMapping("/showAllService")
    public RespResult showAllService(){
       //查询

        List<SetServiceTypeDTO> result=setServiceTypeService.showAllService();

        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return  RespResult.success(result);

    }

//ok
    //服务大厅-根据id显示具体服务,有定价服务
    @GetMapping("/showServiceById")
    public RespResult showServiceById(Integer service,Integer detailsid,Integer ispricing){

        List<ServiceDetails> result=serviceDetailsService.showServiceById(service,detailsid,ispricing);
        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);
    }


//ok
    //显示我的所有房产信息
    @GetMapping("/showAllhouse")
    public RespResult showAllhouse(String userId){

        QueryWrapper houseQueryWrapper = new QueryWrapper<>();
        houseQueryWrapper.eq("customer_id",userId);

        List<House> result=houseService.list(houseQueryWrapper);


        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);
    }
//ok
    //添加房产信息
    @PostMapping("/addHouse")
    public RespResult addHouse(@RequestBody House house){
        System.out.println(house);
       boolean result=houseService.save(house);


        if (result==false){
            return RespResult.error("空");
        }
        return RespResult.success(result);
    }

//ok
    //服务-确认下单

    @PostMapping("/addOrder")
    public RespResult addOrder(@RequestBody ServiceOrder serviceOrder){

        //生成订单唯一序列号
        serviceOrder.setOrderNumber(Long.parseLong(GenerateSequenceUtil.generateSequenceNo()));

        boolean result=serviceOrderService.save(serviceOrder);
        SetService setService=setServices.getById(serviceOrder.getServiceId());
        if (result==true){
            Map map=new HashMap();
            map.put("订单编号",serviceOrder.getOrderNumber());
            map.put("金额",serviceOrder.getMoney());
            map.put("服务名称",setService.getName());
            return RespResult.success(map);
        }
        return RespResult.error("下单失败");

    }

//ok
    //筛选-显示行业和领域
    @GetMapping("/showndustryIAndTerritory")
    public RespResult showndustryIAndTerritory(){

        List<Industry> result =industryService.list();
        List<Field> result1=fieldService.list();

        if (result==null||result.isEmpty()||result1==null||result1.isEmpty()){
            return RespResult.error("空");
        }

        Map<String,List> map=new HashMap();
        map.put("行业",result);
        map.put("领域",result1);
        return RespResult.success(map);
    }

//ok
    //显示所有商海头条,包括高级筛选
    @GetMapping("/showAllheadlines")
    public RespResult showAllheadlines(@RequestBody HeadlinesVo headlinesVo){
        System.out.println(headlinesVo);
        List<Business> result =bussinessService.showAllheadlines(headlinesVo);

        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);
    }


    //根据id查看商海详情,浏览量
    @PostMapping("/showheadlinesById")
    public RespResult showheadlinesById(@RequestBody HeadlinesVo headlinesVo){
        System.out.println(headlinesVo);
        List<Business> result =bussinessService.showAllheadlines(headlinesVo);

        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);
    }



//ok
    //商海-申请合作
    @PostMapping("/applicaation")
    /**
     * businessId商海id
     * userId申请人id
     * time申请时间
     * remarks备注
     */
    public RespResult applicaation( @RequestBody BusinessCustomer businessCustomer){

        boolean result=businessCustomerService.save(businessCustomer);

        if(result==false){
            return RespResult.error("申请失败");
        }
        return RespResult.success(result);
    }

    //商海头条-收藏

//ok
    /**
     *
     * @return
     * 筛选条件
     * 酒店/民宿
     * 根据时间区间筛选
     * 根据评论数排序
     * 评论数相同或无评论，时间排序
     */
    @GetMapping("/stay")
    public RespResult showStay(@RequestBody  AccommodationVo accommodationVo){

        List<Accommodation> result=accommodationService.showStay(accommodationVo);

        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);

    }

//
    //酒店详情，显示房间
    @GetMapping("/showAllRoom")
    public RespResult showAllRoom(@RequestBody RoomVo roomVo){

    List<AccommodationRoomType> result=accommodationRoomTypeService.showAllRoom(roomVo);

        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);
    }


    //根据id查看酒店/民宿评论
    @GetMapping("/shwocommentonByid")
    public RespResult shwoCommentonByid(Integer id, Integer currentPage, Integer pageSize){
        System.out.println(id);
        System.out.println(currentPage);
        List<AccommodationComments> result=accommodationCommentsService.shwoCommentonByid(id,currentPage,pageSize);
        if (result==null||result.isEmpty()){
            return RespResult.error("空");
        }
        return RespResult.success(result);

    }

    //预定-add
    @PostMapping("/reservation")
    public RespResult reservation(@RequestBody ReservationVo reservationVo){
        AccommodationOrder accommodationOrder = new AccommodationOrder();

        accommodationOrder.setCheckInTime(reservationVo.getCheckin());

        accommodationOrder.setCheckOutTime(reservationVo.getCheckout());

        accommodationOrder.setHid(reservationVo.getHid());
        accommodationOrder.setOrderStatus(0);
        accommodationOrder.setOrderTime(reservationVo.getOrderTime());
        accommodationOrder.setUid(reservationVo.getUid());

        accommodationOrder.setRoomType(reservationVo.getRoomType());

        accommodationOrder.setMoney(reservationVo.getOrdermoney());

        String id=GenerateSequenceUtil.generateSequenceNo();
        //生成唯一订单编号
        accommodationOrder.setOrderNum(Integer.parseInt(id));

        //生成订单
        boolean result1=accommodationOrderService.save(accommodationOrder);


        Customer customer = new Customer();
        customer.setId(reservationVo.getUid());
        customer.setPassport(reservationVo.getPassportNum());
        customer.setPassportImage(reservationVo.getPassporturl());

        boolean result2=customerService.updateById(customer);

        if (result1==false||result2==false){
            return RespResult.error("空");
        }
        return RespResult.success(accommodationOrder.getId());

    }




}
